/* Copyright 2014 Pasi J. Elo, blackdogspark@hotmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. 
 */

namespace PJE.GlideEx.Extensions
{
	//Extension methods must be defined in a static class
	public static class ArrayExtensions
	{
		// This is the extension method.
		// The first parameter takes the "this" modifier
		// and specifies the type for which the method is defined.
		/// <summary>
		/// Join short array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this short[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join char array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this char[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join byte array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this byte[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join int array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this int[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join long array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this long[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join double array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this double[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}

		/// <summary>
		/// Join string array with "|" and return string.
		/// </summary>
		/// <returns>string</returns>
		public static string Join(this string[] arr)
		{
			var count = arr.Length;
			var str = "";
			for (var i = 0; i < count; i++)
			{
				str += arr[i];
				if (i < count - 1)
					str += "|";
			}
			return str;
		}
	}

	public static class StringExtensions
	{
		/// <summary>
		/// Split string with "|" and return short array.
		/// </summary>
		/// <returns>Short array</returns>
		public static short[] ToShortArray(this string str)
		{
			var strarr = str.Split('|');
			var count = strarr.Length;
			var arr = new short[count];
			for (var i = 0; i < count; i++)
			{
				var s = short.Parse(strarr[i]);
				arr[i] = s;
			}
			return arr;
		}

		/// <summary>
		/// Split string with "|" and return int array.
		/// </summary>
		/// <returns>int array</returns>
		public static int[] ToIntArray(this string str)
		{
			var strarr = str.Split('|');
			var count = strarr.Length;
			var arr = new int[count];
			for (var i = 0; i < count; i++)
			{
				var s = int.Parse(strarr[i]);
				arr[i] = s;
			}
			return arr;
		}

		/// <summary>
		/// Split string with "|" and return long array.
		/// </summary>
		/// <returns>long array</returns>
		public static long[] ToLongArray(this string str)
		{
			var strarr = str.Split('|');
			var count = strarr.Length;
			var arr = new long[count];
			for (var i = 0; i < count; i++)
			{
				var s = long.Parse(strarr[i]);
				arr[i] = s;
			}
			return arr;
		}

		/// <summary>
		/// Split string with "|" and return double array.
		/// </summary>
		/// <returns>double array</returns>
		public static double[] ToDoubleArray(this string str)
		{
			var strarr = str.Split('|');
			var count = strarr.Length;
			var arr = new double[count];
			for (var i = 0; i < count; i++)
			{
				var s = double.Parse(strarr[i]);
				arr[i] = s;
			}
			return arr;
		}
	}
}